﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.FoundationModel;
using System.Data.Objects;
using MINERP.DAL;
using MINERP.MODEL;
using System.Data;

namespace MINERP.BLL.Statistics
{
    public class WasteBll
    {
        MINERP.DAL.Statistics.WasteDal dal =new  MINERP.DAL.Statistics.WasteDal();
        public List<User_Info> GetUserInfoList()
        {
            List<User_Info> list = new List<User_Info>();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                //tb_Tech tech = entities.tb_Tech.FirstOrDefault(t => t.TechName == "输出管理");
                //if (!tech.users.IsLoaded)
                //{
                //    tech.users.Load();
                //}
                //foreach (var item in tech.users)
                //{
                //    User_Info user_info = new User_Info();
                //    user_info.UserName = item.UserName;
                //    user_info.UserID = item.userID;
                //    user_info.LoginName = item.LoginName;
                //    list.Add(user_info);
                //}
                tb_WorkerGroup group = entities.tb_WorkerGroup.FirstOrDefault(g => g.GroupName.Contains("排版")||g.GroupName.Contains("输出"));
                if (group==null)
                {
                    return list;
                }
                if (!group.users.IsLoaded)
                {
                    group.users.Load();
                }
                foreach (var item in group.users.Where(u=>u.IsUseful))
                {
                    User_Info user_info = new User_Info();
                    user_info.UserName = item.UserName;
                    user_info.UserID = item.userID;
                    user_info.LoginName = item.LoginName;
                    list.Add(user_info);
                }
            }
            return list;
        }
        public List<Printer_Info> GetPrinter()
        {
            List<Printer_Info> rtnPrinter = new List<Printer_Info>();
            rtnPrinter.Add(new Printer_Info());
            ObjectQuery<printer> lisrPrinter = dal.GetAllPriner();
            foreach (printer p in lisrPrinter)
            {
                Printer_Info pi = new Printer_Info() { PrinterID=p.PrinterID,PName=p.PName };

                rtnPrinter.Add(pi);
            }
            return rtnPrinter;
        }

        public List<PaperFace_Info> GetPaperFace()
        {

            List<PaperFace_Info> rtnPaperFace = new List<PaperFace_Info>();
            ObjectQuery<paperFace> lisrPaperFace = dal.GetAllPaperFace();
            foreach (paperFace pf in lisrPaperFace)
            {
                PaperFace_Info pi = new PaperFace_Info() { PaperFaceID= pf.PaperFaceID,Name = pf.Name};
                rtnPaperFace.Add(pi);
            }
            return rtnPaperFace;

        }

        public List<PaperType_Info> GetPaperType()
        {
            List<PaperType_Info> rtnPaperType = new List<PaperType_Info>();
            ObjectQuery<paperType> listPaprtType = dal.GetAllPaperType();
            foreach (paperType pt in listPaprtType)
            {
                PaperType_Info pi = new PaperType_Info() { PaperTypeID = pt.PaperTypeID,Name=pt.Name };
                rtnPaperType.Add(pi);
            }

            return rtnPaperType;
        }

        public DataSet GetWaster( int printrtid,int inPaperFaceId, int inPaperTypeId, DateTime inStarTime, DateTime inEndTime)
        {
            List<Board> boards = dal.GetBoard(printrtid,inPaperTypeId, inPaperFaceId, inStarTime, inEndTime);
            double CoutBoardAreas=0;
            double CountBoardWaste = 0;
            //double CountWasteRate = 0.0;
            double LableAreas = 0.0;
            //double CountLeaberWasterRate = 0.00;
            double PhotoAreas = 0.00;
            foreach (Board b in boards)
            {

                CoutBoardAreas += Convert.ToDouble(b.width * b.length) / b.DPI / b.DPI;
                CountBoardWaste +=Convert.ToDouble( b.width * b.length) / b.DPI / b.DPI * b.WasteRate;
                b.picMakeUp.Load();
                List<picMakeUp> photos = b.picMakeUp.ToList();
                foreach (picMakeUp photo in photos)
                {
                    if (photo.IsWhirl)
                    {
                        LableAreas +=  Convert.ToDouble(photo.width) * photo.labelWidth / b.DPI / b.DPI;
                    }
                    else
                    {
                        LableAreas += Convert.ToDouble(photo.Height) * photo.labelWidth / b.DPI / b.DPI;
                    }
                    PhotoAreas += Convert.ToDouble(photo.width) * photo.Height / b.DPI / b.DPI;
                }
 
            }
            DataTable  table = CreateWasterRate();
            DataRow row = table.NewRow();
            row["CoutBoardAreas"]=CoutBoardAreas*2.54*2.54/10000;
            row["CountBoardWaste"] = CountBoardWaste * 2.54 * 2.54 / 10000;
            row["LableAreas"] = LableAreas * 2.54 * 2.54 / 10000;
            row["PhotoAreas"] = PhotoAreas * 2.54 * 2.54 / 10000;
            row["CountWasteRate"] = (LableAreas + CountBoardWaste) / CoutBoardAreas*100;
            row["CountLeaberWasterRate"] = LableAreas / CoutBoardAreas*100;

            table.Rows.Add(row);
            table.TableName="table";
            
            DataSet set = new DataSet();
            set.Tables.Add(table);
            set.WriteXml(@"C:\windows\xmlData\Waste.xml",XmlWriteMode.WriteSchema);

            return set;
            //CountWasteRate = photoAreas / CoutBoardAreas;

            //CountLeaberWasterRate = LableAreas / CoutBoardAreas;

        }
        public DataSet GetWasterNew(List<string> userNameList, DateTime inStarTime, DateTime inEndTime)
        {
            DataTable table = CreateWasterRate();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                foreach (var userName in userNameList)
                {
                    double CoutBoardAreas = 0;
                    double CountBoardWaste = 0;
                    double CoutLableAreas = 0;
                    double CoutPhotoAreas = 0;

                    List<PBStatistic> PBStatisticList = entities.PBStatistic.Where(p => p.Operator == userName && p.DateTime >= inStarTime && p.DateTime <=inEndTime).ToList();
                    foreach (var item in PBStatisticList)
                    {
                        CoutBoardAreas += item.BoardArea;
                        CountBoardWaste += (item.BoardArea * item.Waste);
                        CoutLableAreas += item.LabelArea;
                        //CoutPhotoAreas = CoutBoardAreas - CountBoardWaste - CoutPhotoAreas;   
                    }
                    CoutPhotoAreas = CoutBoardAreas - CountBoardWaste - CoutLableAreas;
                    DataRow row = table.NewRow();
                    row["userName"] = userName;
                    row["CoutBoardAreas"] = CoutBoardAreas;
                    row["CountBoardWaste"] = CountBoardWaste;
                    row["LableAreas"] = CoutLableAreas;
                    row["PhotoAreas"] = CoutPhotoAreas;
                    row["CountWasteRate"] = (CoutLableAreas + CountBoardWaste) / CoutBoardAreas * 100;
                    row["CountLeaberWasterRate"] = CoutLableAreas / CoutBoardAreas * 100;
                    table.Rows.Add(row);
                }

            }

            table.TableName = "table";

            DataSet set = new DataSet();
            set.Tables.Add(table);
            set.WriteXml(@"C:\windows\xmlData\Waste.xml", XmlWriteMode.WriteSchema);

            return set;

        }
        public DataTable CreateWasterRate()
        {

            DataTable  table =new DataTable();
            table.Columns.Add("userName", typeof(string));
            table.Columns.Add("CoutBoardAreas",typeof(double));
            table.Columns.Add("CountBoardWaste",typeof(double));
            table.Columns.Add("LableAreas",typeof(double));
            table.Columns.Add("PhotoAreas",typeof(double));
            table.Columns.Add("CountLeaberWasterRate", typeof(double));
            table.Columns.Add("CountWasteRate", typeof(double));
            return table;

            //public double CoutBoardAreas;//板面积
            //public double CountBoardWaste;//浪费总面积
            //double CountWasteRate;//总浪费率
            //public double LableAreas; //留白总面积
            //double CountLeaberWasterRate;//留白浪费率
            //public double PhotoAreas;//照片有效面积
        }
    }
   
}
